using System.Threading.Tasks;
using DG.Tweening;
using UnityEngine;
using Voodoo.GameOps;
using Voodoo.Pattern;

public class TransitionAnimationController : SingletonMB<TransitionAnimationController>
{
	public Vector3 transitionEndPosition;

	public Vector3 transitionStartPosition;

	public Transform CameraPosition;

	public Transform CameraStartPosition;

	public Transform CameraEndPosition;

	public Transform startPosition;

	public Transform endPosition;

	public Transform headParent;

	private GameObject headVisual;

	public Animator lockAnim;

	public TransitionMachine Machine;

	public GameObject CoinExplosionEffect;

	public GameObject TrailEffect;

	public Transform tubeObject;

	public GameObject gameCompleteCanvas;

	private void Awake()
	{
		Object.DontDestroyOnLoad(base.gameObject);
		transitionEndPosition = base.transform.position;
	}

	public async void StartTransition()
	{
		SingletonMB<GameManager>.Instance.StartLoadIntoTransition();
		TrailEffect.SetActive(value: false);
		Camera mainCam = SingletonMB<GameManager>.Instance.Cam.GetComponent<Camera>();
		SingletonMB<UIManager>.Instance.HideAllUI();
		WatchCoins();
		SingletonMB<GameManager>.Instance.Cam.enabled = false;
		mainCam.depth = 0f;
		mainCam.transform.parent = base.transform;
		mainCam.transform.DOMove(CameraStartPosition.position, 1f);
		await UsefulExtensions.WaitSeconds(1f);
		lockAnim.SetBool("Trigger", value: true);
		SingletonMB<GameManager>.Instance.GameplayHaptic.PlayHaptic(HapticType.Big);
		mainCam.DOShakePosition(1f, 0.75f);
		gameCompleteCanvas.SetActive(value: true);
		await Task.Delay(1500);
		mainCam.transform.DOMove(CameraPosition.position, 1f).SetEase(Ease.OutSine);
		await Task.Delay(1000);
		headVisual = Object.Instantiate(InventoryManager.Instance.CurrentlySelected(ItemType.Skin).HeadObject, headParent);
		headParent.transform.position = startPosition.position;
		headVisual.transform.localPosition = Vector3.zero;
		headVisual.GetComponent<Animator>().enabled = false;
		Rotate rotate = headVisual.AddComponent<Rotate>();
		rotate.speed = 1f;
		rotate.eulerAngles = new Vector3(0f, 180f, 0f);
		float scale = (0.35f + (float)NintendoPlayerPrefs.GetInt("Size", 0) * 0.05f) * (1f + (float)NintendoPlayerPrefs.GetInt("ScinSize", 0) / 100f);
		headParent.transform.localScale = new Vector3(scale, scale, scale);
		TrailEffect.SetActive(value: true);
		ShortcutExtensions.DOLocalMove(endValue: new Vector3(CameraPosition.localPosition.x, CameraPosition.localPosition.y, headParent.localPosition.z), target: headParent, duration: 1f).SetEase(Ease.OutSine);
		await Task.Delay(1500);
		float timer = 0f;
		float endSize = 0.35f * (1f + (float)NintendoPlayerPrefs.GetInt("ScinSize", 0) / 100f);
		SingletonMB<GameManager>.Instance.Cam.enabled = false;
		Vector3 camStartPosition = CameraPosition.position;
		Vector3 camEndPosition = CameraEndPosition.position;
		while (timer < 1f)
		{
			timer += Time.deltaTime;
			mainCam.transform.position = Vector3.Lerp(camStartPosition, camEndPosition, timer);
			float num = Mathf.Lerp(scale, endSize, timer);
			headParent.transform.localScale = new Vector3(num, num, num);
			await Task.Yield();
		}
		NintendoPlayerPrefs.SetInt("Play_Transition", 1);
		SingletonMB<Shop>.Instance.ResetAllSells();
		SingletonMB<GameManager>.Instance.LoadIntoTransition();
		gameCompleteCanvas.SetActive(value: false);
		Object.Destroy(mainCam.GetComponent<Cam>());
		await Task.Delay(500);
		while (SingletonMB<GameManager>.Instance.Cam._head == null)
		{
			await Task.Yield();
		}
		await Task.Yield();
		NintendoPlayerPrefs.SetInt("Play_Transition", 0);
		base.transform.position = SingletonMB<GameManager>.Instance.Cam._head.position + new Vector3(0f, 27.5f, 0f);
		Vector3 endValue2 = new Vector3(SingletonMB<GameManager>.Instance.Cam._head.position.x, SingletonMB<GameManager>.Instance.Cam._head.position.y, SingletonMB<GameManager>.Instance.Cam._head.position.z);
		mainCam.transform.parent = headParent.transform;
		headParent.DOMove(endValue2, 1f).SetEase(Ease.InSine).OnComplete(delegate
		{
			TrailEffect.SetActive(value: false);
			mainCam.DOShakePosition(1f, 0.75f);
			SingletonMB<GameManager>.Instance.GameplayHaptic.PlayHaptic(HapticType.Big);
		});
		await Task.Delay(1500);
		await Task.Delay(500);
		mainCam.transform.parent = null;
		CameraFollow(mainCam);
		tubeObject.DOMove(tubeObject.transform.position + new Vector3(0f, 10f, 0f), 2f);
		await Task.Delay(1500);
		SingletonMB<GameManager>.Instance.Cam._head.gameObject.SetActive(value: true);
		SingletonMB<UIManager>.Instance.AllUI.alpha = 1f;
		Object.Destroy(base.gameObject);
	}

	private async void CameraFollow(Camera mainCam)
	{
		for (float timer = 0f; timer < 2f; timer += Time.deltaTime)
		{
			mainCam.transform.position = Vector3.Lerp(mainCam.transform.position, SingletonMB<GameManager>.Instance.Cam.transform.position, 0.1f);
			mainCam.fieldOfView = Mathf.Lerp(mainCam.fieldOfView, SingletonMB<GameManager>.Instance.Cam.cameraObject.fieldOfView, 0.1f);
			await Task.Yield();
		}
		while (Vector3.Distance(mainCam.transform.position, SingletonMB<GameManager>.Instance.Cam.transform.position) < 0.1f || Mathf.Abs(mainCam.fieldOfView - SingletonMB<GameManager>.Instance.Cam.cameraObject.fieldOfView) > 0.1f)
		{
			mainCam.transform.position = Vector3.Lerp(mainCam.transform.position, SingletonMB<GameManager>.Instance.Cam.transform.position, 0.1f);
			mainCam.fieldOfView = Mathf.Lerp(mainCam.fieldOfView, SingletonMB<GameManager>.Instance.Cam.cameraObject.fieldOfView, 0.1f);
			await Task.Yield();
		}
		Object.Destroy(mainCam.gameObject);
	}

	private async void WatchCoins()
	{
		while (Machine.SpawnedCoins.Count > 0)
		{
			await Task.Yield();
			for (int i = 0; i < Machine.SpawnedCoins.Count; i++)
			{
				if (Machine.SpawnedCoins[i].transform.position.y < CameraPosition.position.y + Mathf.Lerp(1f, 2f, (float)i / (float)Machine.SpawnedCoins.Count))
				{
					Object.Instantiate(CoinExplosionEffect, Machine.SpawnedCoins[i].transform.position + new Vector3(0f, 0f, -6f), Quaternion.identity, base.transform);
					Object.Destroy(Machine.SpawnedCoins[i].gameObject);
					Machine.SpawnedCoins.RemoveAt(i);
				}
			}
		}
	}

	private void Update()
	{
		if (Input.GetKeyDown(KeyCode.T))
		{
			StartTransition();
		}
	}
}
